/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import domen.User;
import ejb.UserFacade;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import managed.util.LogInOutManager;
import managed.util.Users;

/**
 *
 * @author Danilo
 */
@ManagedBean(name="loginManagedBean")
@RequestScoped
public class LoginManagedBean {

    @EJB
    private UserFacade ufr;
    @EJB
    private Users users;
    
    private String username;
    private String password;
    private String sessionId;

    /** Creates a new instance of LoginManagedBean */
    public LoginManagedBean() {
    }

    public String login() {
        User user = ufr.find(getUsername());
        if(user!=null){
            if(users.getOnlineUsers().containsKey(username)){
                String temp = users.getOnlineUsers().get(username);
                HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                if(temp.equals(ses.getId())){
                return "/secure/home.xhtml?faces-redirect=true";
                }
               FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_WARN, "Greska pri logovanju!", "Korisnik sa korisnickim imenom "+username+" je vec prijavljen!"));
                restart();
                return null; 
            }else if (user.getPassword().equals(password)) {
            if (user.getActivated()) {
                LogInOutManager.login(user);
                HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                users.getOnlineUsers().put(username, ses.getId());
                return "/secure/home.xhtml?faces-redirect=true";
            } else if (user.getSessionId().equalsIgnoreCase(getSessionId())) {
                LogInOutManager.login(user);
                user.setActivated(true);
                ufr.edit(user);
                return "/secure/home.xhtml?faces-redirect=true";
            }else{
                FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_WARN, "Greska pri logovanju!", "Vas nalog jos uvek nije aktiviran. Proverite vas email."));
                restart();
                return null;
            }
        } else{
            FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_INFO, "Greska pri logovanju!", "Pogresno uneto korisnicko ime ili sifra!"));
            restart();
            return null;
        }
    } 
        FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_INFO, "Greska pri logovanju!", "Pogresno uneto korisnicko ime ili sifra!"));
        restart();
        return null;
    }

    public String logout(String username){
        LogInOutManager.logout(getUsername());
        users.getOnlineUsers().remove(username);
        return "logout";
    }

    public String register(){
        
        return "register";
    }
    

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the sessionId
     */
    public String getSessionId() {
        return sessionId;
    }

    /**
     * @param sessionId the sessionId to set
     */
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    
    private void restart(){
        username = "";
        password = "";
    }
}
